#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

function configureHiveEnv()
{
 hiveEnvUrl=$1
 hive_home=$2
 
 cp $hiveEnvUrl.template $hiveEnvUrl

 profile=/etc/profile.d/my_env.sh
 java_home=`egrep "^export JAVA_HOME=" $profile`
 hadoop_home=`egrep "^export HADOOP_HOME=" $profile`
 
 echo "$java_home" >> $hiveEnvUrl
 echo "$hadoop_home" >> $hiveEnvUrl
 echo "export HIVE_CONF_DIR=$hive_home/conf" >> $hiveEnvUrl

}

# 配置hive-site.xml
function configureHiveSite()
{
 hiveSiteUrl=$1
 mysqlNode=`egrep "^hive-mysql-node" $PLAY_HOME/configs.txt | cut -d " " -f2 | sed s/\r//`
 mysqlHivePasswd=`egrep "^mysql-hive-password" $PLAY_HOME/configs.txt | cut -d " " -f2 | sed s/\r//`
 
 # 从frames.txt中获取hive安装包信息
 hiveInfo=`egrep "hive" $PLAY_HOME/frames.txt`
 hivePackage=`echo $hiveInfo | cut -d " " -f1`
 hiveVersion=`echo $hivePackage | grep -oP "apache-hive-\K[0-9]+\.[0-9]+\.[0-9]+" || echo ""`
 majorVersion=`echo $hiveVersion | cut -d. -f1`
 
 # 清空MySQL中的Hive Metastore
 ssh $mysqlNode "source /etc/profile && export MYSQL_PWD=$mysqlHivePasswd && mysql --connect-expired-password -uroot -e \"drop database if exists hive;\""

 cat >> $hiveSiteUrl <<EOF
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://$mysqlNode:3306/hive?createDatabaseIfNotExist=true&amp;useSSL=false</value>
    <description>JDBC connect string for a JDBC metastore</description>
  </property>
 
  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>
 
  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>hive</value>
    <description>username to use against metastore database</description>
  </property>
 
  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>$mysqlHivePasswd</value>
    <description>password to use against metastore database</description>
  </property>

  <property>
    <name>hive.metastore.schema.verification</name>
    <value>false</value>
  </property>

  <property>
    <name>datanucleus.schema.autoCreateTables</name>
    <value>true</value>
    <description>
      MetaStore表如果不存在，则自动进行创建
    </description>
  </property>
  
  <property>
    <name>hive.compactor.initiator.on</name>
    <value>true</value>
    <description>
      开启事务必要配置：是否在Metastore上启动初始化和清理线程
    </description>
  </property>

  <property>
    <name>hive.compactor.worker.threads</name>
    <value>3</value>
    <description>
      开启事务必要配置：压缩时，启动的worker线程数
    </description>
  </property>

  <property>
    <name>hive.support.concurrency</name>
    <value>true</value>
    <description>
      开启事务必要配置：HiveServer2 Client配置，是否允许并发
    </description>
  </property>

  <property>
    <name>hive.txn.manager</name>
    <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
    <description>
      开启事务必要配置：HiveServer2 Client配置，提供事务行为
    </description>
  </property>
EOF

 # 根据版本添加不同的配置
 if [ "$majorVersion" = "4" ]; then
   # 4.x版本配置
   cat >> $hiveSiteUrl <<EOF
  <property>
    <name>datanucleus.autoStartMechanismMode</name>
    <value>ignored</value>
  </property>
EOF
 elif [ "$majorVersion" = "3" ]; then
   # 3.x版本配置  
   cat >> $hiveSiteUrl <<EOF
  <property>
    <name>datanucleus.autoStartMechanismMode</name>
    <value>ignored</value>
  </property>
EOF
 else
   # 1.x和2.x版本配置
   cat >> $hiveSiteUrl <<EOF
  <property>
    <name>hive.enforce.bucketing</name>
    <value>true</value>
    <description>
      开启事务必要配置：HiveServer2 Client配置，开启自动分桶
      在2.x之后不需要进行配置，为了兼容之前版本添加此参数
    </description>
  </property>
EOF
 fi

 cat >> $hiveSiteUrl <<EOF
</configuration>
EOF

}

# 配置hive-site.xml，开启Thrift服务
configureHiveThrift()
{
hiveSite=$1
hiveNode=`hostname`
  
sed -i "s#</configuration>##g" $hiveSite

cat >> $hiveSite <<EOF
  <property>
  <name>hive.server2.thrift.port</name>
  <value>10000</value>
  </property>
  <property>
  <name>hive.server2.thrift.bind.host</name>
  <value>$hiveNode</value>
  </property>
  <property>
  <name>hive.metastore.uris</name>
  <value>thrift://$hiveNode:9083</value>
  </property>
  </configuration>
EOF
}

function installHive()
{
 #在frames.txt中查看是否需要安装hive
 hiveInfo=`egrep "hive" $PLAY_HOME/frames.txt`
 
 hive=`echo $hiveInfo | cut -d " " -f1`
 isInstall=`echo $hiveInfo | cut -d " " -f2`
 hiveNode=`echo $hiveInfo | cut -d " " -f3`
 node=`hostname`
 
 #是否在当前节点进行安装
 if [[ $isInstall = "true" && $hiveNode = $node ]];then
     
     #查看$PLAY_HOME/frames目录下是否有hive安装包
     hiveIsExists=`find $PLAY_HOME/frames -name $hive`
    
     if [[ ${#hiveIsExists} -ne 0 ]];then
           
          if [[ ! -d /opt/app ]];then
              mkdir /opt/app && chmod -R 775 /opt/app
          fi
   
          #删除旧的
          hive_home_old=`find /opt/app -maxdepth 1 -name "*hive*"`
          for i in $hive_home_old;do
                rm -rf $i
          done

          #解压到指定文件夹/opt/app中
          echo "开始解压hive安装包"
          tar -zxvf $hiveIsExists -C /opt/app >& /dev/null
          echo "hive安装包解压完毕"

          hive_home=`find /opt/app -maxdepth 1 -name "*hive*"`
 
          #配置hive-env.sh文件
          configureHiveEnv $hive_home/conf/hive-env.sh $hive_home

          #配置hive-log4j2.properties文件
          cp $hive_home/conf/hive-log4j2.properties.template $hive_home/conf/hive-log4j2.properties

          #配置hive-site.xml
          configureHiveSite $hive_home/conf/hive-site.xml
          
          #拷贝Mysql连接驱动
          mysqlDrive=`egrep "^mysql-drive" $PLAY_HOME/configs.txt | cut -d " " -f2`
          lib_home="$PLAY_HOME/frames/lib"
          #判断驱动是否存在
          driveIsExists=`find $lib_home -name $mysqlDrive`
          if [[ ${#driveIsExists} -ne 0 ]];then
            cp $lib_home/$mysqlDrive $hive_home/lib/
          else
            echo "$lib_home目录下没有Mysql驱动"
          fi

          #为Presto开启Thrift服务
          prestoInstall=`egrep "presto" $PLAY_HOME/frames.txt | cut -d " " -f2`
          if [[ $prestoInstall = "true" ]];then
            echo "开始配置Hive Thrift服务"
            configureHiveThrift $hive_home/conf/hive-site.xml
            # 开启Hive元数据服务
            # hive --service hiveserver2 &
            # hive --service metastore &
          fi

          # 初始化元数据
          $hive_home/bin/schematool -dbType mysql -initSchema
   
          #配置HIVE_HOME
          profile=/etc/profile.d/my_env.sh
          sudo sed -i "/^export HIVE_HOME/d" $profile
          echo "export HIVE_HOME=$hive_home" | sudo tee -a $profile

          #配置PATH
          sudo sed -i "/^export PATH=\$PATH:\$HIVE_HOME\/bin/d" $profile
          echo "export PATH=\$PATH:\$HIVE_HOME/bin" | sudo tee -a $profile

          #更新/etc/profile文件
          source /etc/profile && echo "更新/etc/profile环境变量完成"

          # 判断是否安装Tez
          tezInstall=`egrep "tez" $PLAY_HOME/frames.txt | cut -d " " -f2`
          if [[ $tezInstall = "true" ]];then
            echo "开始安装Tez服务"
            $PLAY_HOME/hadoop/installTez.sh
          fi
          # alter database hive character set latin1;
          # 输出提示信息
          echo "--------------------------"
          echo "|   Hive在$node安装成功！  |"
          echo "--------------------------"
          echo "Hive服务启动命令: hive"
          if [[ $prestoInstall = "true" ]];then
          echo "为Presto开启Hive元数据服务命令: hive --service hiveserver2 &"
          echo "为Presto开启Hive元数据服务命令: hive --service metastore &"
          fi
          if [[ $tezInstall = "true" ]];then
              echo "Hive将运行在Tez引擎之上"
          fi
     else
         echo "$PLAY_HOME/frames目录下没有hive安装包"
     fi
 else
     echo "Hive不允许被安装在当前节点"
 fi
}

installHive
